package com.partner.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.partner.boot.entity.Chat;
import com.partner.boot.mapper.ChatMapper;
import com.partner.boot.service.IChatService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 菜牙买菜
 * @since 2023-02-10
 */
@Service
public class ChatServiceImpl extends ServiceImpl<ChatMapper, Chat> implements IChatService {

    @Resource
    ChatMapper chatMapper;

    /**
     * 获取初识页面的信息数组
     * @param limit 要初始化的信息数量
     * @return
     */
    @Override
    public List<Chat> findAllInit(Integer limit) {

        if(limit == null) {
            limit = 10;
        }
        // 先查询后limit条的数据，降序获取最新的数据
        List<Chat> list = chatMapper.selectList(new QueryWrapper<Chat>().orderByDesc("id").last("limit " + limit));
        // 最后正序转成数组返回
        return list.stream().sorted(Comparator.comparing(Chat::getId)).collect(Collectors.toList());
    }
}
